#include "steering.h"
#include "drivers/dev_pwm.h"
#include "rtdef.h"
#include "rttypes.h"
#include <stdint.h>

#define LOG_TAG              "steering"
#define LOG_LVL              LOG_LVL_DBG
#include <ulog.h>

#define PWM_DEV_NAME        "pwm1"  /* PWM设备名称 */
#define PWM_CHANNEL         1   /* PWM通道 */

struct rt_device_pwm *pwm_dev_steering;      /* PWM设备句柄 */

void pwm_set_steering_pulse(rt_uint32_t pulse){
    rt_uint32_t peri =50000000;
    if (pulse>50000000)
    {
        pulse =50000000;
    }
    if (pulse <0)
    {
        pulse =0;
    }
    rt_pwm_set(pwm_dev_steering,PWM_CHANNEL,peri,pulse);
}




int steering_init(void){
    rt_err_t ret = RT_EOK;

    /* 查找设备 */
    pwm_dev_steering = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
    if (pwm_dev_steering == RT_NULL)
    {
        LOG_E("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
        ret = -RT_ERROR;
    }
    
    rt_pwm_enable(pwm_dev_steering, PWM_CHANNEL);
    return ret;
}

INIT_APP_EXPORT(steering_init);